package com.wms.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wms.entity.MyPage;
import com.wms.service.BaseService;
import org.mybatis.spring.SqlSessionTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

    @Resource(name = "sqlSessionTemplate")
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public IPage<T> queryPage(String mapper, MyPage<T> page) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("size", page.getSize());
        params.put("offset", page.getOffset());
        params.put("condition", page.getCondition());
        params.put("orderByClause", page.getOrderByClause());

        Long count = sqlSessionTemplate.selectOne(mapper + "Count", params);
        page.setTotal(count == null ? 0L : count);

        if (page.getCurrent() <= page.getPages()){
            List<T> list = sqlSessionTemplate.selectList(mapper, params);
            page.setRecords(list);
        }
        return page;
    }
}
